package com.itheima.app.controller;

import com.itheima.app.interceptor.UserHolder;
import com.itheima.app.manager.VideoManager;
import com.itheima.vo.PageBeanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.ws.rs.POST;
import java.util.Map;

@RestController
public class VideoController {

    @Autowired
    private VideoManager videoManager;

    // 推荐视频查询
    @GetMapping("/smallVideos")
    public ResponseEntity findRecommendVideoVoByPage(
            @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        // 获取登录用户id
        Long userId = UserHolder.get().getId();
        // 调用manager
        PageBeanVo pageBeanVo = videoManager.findRecommendVideoVoByPage(userId, pageNum, pageSize);
        return ResponseEntity.ok(pageBeanVo);
    }

    // 发布视频
    @PostMapping("/smallVideos")
    public void publishVideo(MultipartFile videoThumbnail, MultipartFile videoFile) throws Exception {
        // 获取登录用户id
        Long userId = UserHolder.get().getId();
        // 调用manager
        videoManager.publishVideo(userId, videoThumbnail, videoFile);
    }

    // 关注
    @PostMapping("/smallVideos/{focusUserId}/userFocus")
    public void userFocus(@PathVariable Long focusUserId) {
        // 获取登录用户id
        Long userId = UserHolder.get().getId();
        // 调用manager
        videoManager.userFocus(userId, focusUserId);
    }

    // 取消关注
    @PostMapping("/smallVideos/{focusUserId}/userUnFocus")
    public void userUnFocus(@PathVariable Long focusUserId) {
        // 获取登录用户id
        Long userId = UserHolder.get().getId();
        // 调用manager
        videoManager.userUnFocus(userId, focusUserId);
    }

    // 视频点赞
    @PostMapping("/smallVideos/{videoId}/like")
    public ResponseEntity videoLike(@PathVariable String videoId) {
        // 获取登录用户id
        Long userId = UserHolder.get().getId();
        // 调用manager
        return videoManager.videoLike(userId, videoId);
    }

    // 视频点赞 - 取消
    @PostMapping("/smallVideos/{videoId}/dislike")
    public ResponseEntity videoDislike(@PathVariable String videoId) {
        // 获取登录用户id
        Long userId = UserHolder.get().getId();
        // 调用manager
        return videoManager.videoDislike(userId, videoId);
    }

    //评论列表
    @GetMapping("/smallVideos/{videoId}/comments")
    public ResponseEntity findVideoCommentsByPage(@PathVariable String videoId,
                                                  @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
                                                  @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {


        return videoManager.findVideoCommentsByPage(videoId, pageNum, pageSize);
    }


    // 评论发布
    @PostMapping("/smallVideos/{videoId}/comments")
    public void saveVideosComments(@PathVariable String videoId, @RequestBody Map<String,String> map)  {
        String comment = map.get("comment");
        videoManager.saveVideosComments(videoId, comment);
    }

    //评论点赞
    @PostMapping("/smallVideos/comments/{videosCommentLikeId}/like")
    public ResponseEntity videosCommentLike(@PathVariable String videosCommentLikeId)  {
        return videoManager.videosCommentLike(videosCommentLikeId);
    }


    //评论点赞 - 取消
    @PostMapping("/smallVideos/comments/{videosCommentLikeId}/dislike")
    public ResponseEntity videosCommentDislike(@PathVariable String videosCommentLikeId)  {
        return videoManager.videosCommentDislike(videosCommentLikeId);
    }


}